<script setup>
import router from "@/router";
// do not use same name with ref
import { ref } from 'vue'
import { Schedules } from "@/store/Schedule";

const schedules = Schedules().schedules;

const handleClose = (task) => {
  const index = schedules.findIndex(schedule => schedule.task === task);
  if (index !== -1) {
    schedules.splice(index, 1); // 删除指定的行程
  }
}

const addSchedule = (task) => {
  router.push("/travel/AddSchedule");
}

const editSchedule = (task) => {
  router.push({
    path: '/travel/EditSchedule',
    query: { task: task } // 传递行程ID作为查询参数
  });
}

const onSubmit = () => {
  console.log('submit!');
  router.push("/travel/BudgetRequest");
}

const returnLast = () => {
  console.log('submit!');
  router.push("/travel/ApplyForTravel");
}
</script>

<template>
  <div class="main-container">
    <div class="form-container">
      <el-steps style="max-width: 100%" :active="2" finish-status="success" align-center>
        <el-step title="查看差旅规章制度" description="View rules" />
        <el-step title="申请出差" description="Apply for travel" />
        <el-step title="规划行程" description="Plan schedules" />
        <el-step title="预算申请" description="Budget request" />
        <el-step title="出差申请完成" description="Success" />
      </el-steps>
      <el-divider />

      <div style="font-size: 25px; color: #009dff; font-weight: bolder; width: 100%; text-align:center; margin: 0 0 20px 0">规 划 行 程</div>
      <div class="flex-gap-2">
        <el-tag
            class="ScheduleButton"
            v-for="tag in schedules"
            :key="tag.task"
            closable
            :disable-transitions="false"
            @close="handleClose(tag.task)"
            @click="editSchedule(tag.task)"
        >
          {{ tag.task }}
        </el-tag>
        <el-button class="ScheduleButton" size="small" @click="addSchedule">
          + 添加新行程
        </el-button>
      </div>
      <el-form-item style="width: 100%; align-items: center">
        <div style="margin: 10px auto 0;">
          <el-button class="button" type="primary" @click="onSubmit" size="large">下一步</el-button>
          <el-button class="button" size="large" @click="returnLast">上一步</el-button>
        </div>
      </el-form-item>
    </div>
  </div>

</template>

<style>
.main-container{
  width: 100%;
}
.form-container{
  margin: 10px auto auto;
  width: 55%;
  padding: 2% 5%;
  border-radius: 20px;
  box-shadow: 0px 0px 15px 0px rgba(122, 122, 122, 0.3);
  -webkit-box-shadow: 0px 0px 15px 0px rgba(122, 122, 122, 0.3);
  -moz-box-shadow: 0px 0px 15px 0px rgba(122, 122, 122, 0.3);
}
.form{
  margin: 0 auto;
}
.ScheduleButton{
  width: 200px;
  height: 40px;
  margin: 5px;
  cursor: pointer;
}
.button{
  margin: 0 auto;
}
.flex-gap-2{
  border: 1px solid #c0c0c0;
  border-radius: 20px;
  min-height: 200px;
  padding: 5%;
}
</style>
